package user

import (
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"time"
	"wx-api/dsp-api/common"
	"wx-api/dsp-api/config"
)

type DspUser struct {
	Id         bson.ObjectId `bson:"_id,omitempty" json:"id,omitempty"`
	Name       string        `bson:"name" json:"name,omitempty"`               //名称
	Email      string        `bson:"email" json:"email,omitempty"`             //邮箱
	Phone      string        `bson:"phone" json:"phone,omitempty"`             //手机
	Password   string        `bson:"password" json:"password,omitempty"`       //密码
	Flag       int           `bson:"flag" json:"flag,omitempty"`               //1 开启 2 关闭
	Status     int           `bson:"status" json:"status,omitempty"`           //1 正常 2 删除
	CreateTime int64         `bson:"create_time" json:"create_time,omitempty"` //创建时间
	UpdateTime int64         `bson:"update_time" json:"update_time,omitempty"` //更新时间
	Version    string        `bson:"version" json:"version,omitempty"`         //版本号
}

func dbGet() *common.TkMongo {
	return config.MgTableDspUser
}

func Save(a DspUser) error {
	a.Id = bson.NewObjectId()
	tmptime := time.Now().Unix()
	a.CreateTime = tmptime
	a.UpdateTime = tmptime
	a.Flag = 1
	a.Status = 1
	a.Version = config.SysVersion
	return dbGet().Insert(a)
}

// 更新
func Update(a DspUser) error {
	m := bson.M{
		"name":        a.Name,
		"update_time": time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": a.Id}, m)
}

// 更新状态
func UpdateFalg(id string, flag int) error {
	m := bson.M{
		"flag":        flag,
		"update_time": time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": bson.ObjectIdHex(id)}, m)
}

// 删除
func UpdateDel(id string, status int) error {
	m := bson.M{
		"status":      status,
		"update_time": time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": bson.ObjectIdHex(id)}, m)
}

// 获取所有
func GetAll() []DspUser {
	list := []DspUser{}
	_ = dbGet().All(nil, &list)
	return list
}
func GetWAll(a int64) []DspUser {
	list := []DspUser{}
	_ = dbGet().All(bson.M{"update_time": bson.M{"$gte": a}}, &list)
	return list
}

// 获取详情
func GetByCode(id string) (*DspUser, error) {
	a := &DspUser{}
	err := dbGet().FindOne(bson.M{"_id": bson.ObjectIdHex(id)}, a)
	if err != nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}
	return a, nil
}

// 根据邮箱查询
func SeachEmail(email string) (DspUser, bool) {
	aa := DspUser{}
	w := bson.M{"email": email}
	err := dbGet().FindOne(w, &aa)
	if err != nil {
		if err == mgo.ErrNotFound {
			return aa, false
		}
		return aa, false
	}
	return aa, true
}

// 列表，带分页
func List(id, name, email string, flag, status, pageNum, pageSize int) (*common.PageInfo, error) {
	s := bson.M{}
	if id != "" {
		s["_id"] = bson.ObjectIdHex(id)
	}
	if name != "" {
		s["name"] = bson.M{"$regex": bson.RegEx{name, "i"}}
	}
	if email != "" {
		s["email"] = email
	}
	if flag > 0 {
		s["flag"] = flag
	}
	if status > 0 {
		s["status"] = status
	}
	var list []DspUser
	return dbGet().FindPageInfo(s, "", pageSize, pageNum, "-update_time", &list)
}
